import { login } from '@/api/login.js'
import { getToken, setToken, removeToken } from '@/utils/localStorage.js'
const state = {
  token: getToken() || ''
}
const mutations = { // 同步
  setToken (state, payload) {
    state.token = payload
  }
}
const actions = { // 异步更改
  async LoginAction ({ commit }, payload) {
    const data = await login(payload)
    console.log(data.token)
    commit('setToken', data.token)
    setToken(data.token) // 本地存储token
    return data
  },
  LoginOut ({ commit }) {
    commit('setToken', '')
    removeToken()
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
